//
//  HUContactModelManager.m
//  获得手机通讯录
//
//  Created by hu on 16/8/2.
//  Copyright © 2016年 hu. All rights reserved.
//

#import "HUContactModelManager.h"
@import ObjectiveC;

static NSString * const HUContactsManager;

@implementation HUContactModelManager
+(ABRecordRef)recordRef
{
    return (__bridge ABRecordRef)(objc_getAssociatedObject(self, &HUContactsManager));
}

+(void)setRecordRef:(ABRecordRef)recordRef
{
    objc_setAssociatedObject(self, &HUContactsManager, (__bridge id)(recordRef), OBJC_ASSOCIATION_ASSIGN);
}

-(void)dealloc
{
    objc_removeAssociatedObjects(self);
}

+ (HUContractModel *)contantObject:(ABRecordRef)recordRef
{
    //设置当前的值
    self.recordRef = recordRef;
    
    //初始化一个YContactObject对象
    HUContractModel * contactObject = [[HUContractModel alloc]init];
    
    //姓名对象
    contactObject.nameObject = [self contactNameProperty];
    
  
    //头像
    contactObject.headImage = [self contactHeadImagePropery];
    
    //电话对象
    contactObject.phoneObject = [self contactPhoneProperty];
    
    
   
    //备注
    contactObject.note = [self contactProperty:kABPersonNoteProperty];                                  //备注
    
    //创建时间
    contactObject.creationDate = [self contactDateProperty:kABPersonCreationDateProperty];              //创建日期
    
    //最近一次修改的时间
    contactObject.modificationDate = [self contactDateProperty:kABPersonModificationDateProperty];      //最近一次修改的时间
    
    return contactObject;
}


/**
 *  根据属性key获得NSString
 *
 *  @param property 属性key
 *
 *  @return 字符串的值
 */
+ (NSString *)contactProperty:(ABPropertyID) property
{
    return (__bridge NSString *)(ABRecordCopyValue(self.recordRef, property));
}


/**
 *  根据属性key获得NSDate
 *
 *  @param property 属性key
 *
 *  @return NSDate对象
 */
+ (NSDate *)contactDateProperty:(ABPropertyID) property
{
    return (__bridge NSDate *)(ABRecordCopyValue(self.recordRef, property));
}



/**
 *  获得姓名的相关属性
 */
+ (HUContactNameObject *)contactNameProperty
{
    
    HUContactNameObject * nameObject = [[HUContactNameObject alloc]init];
    
    nameObject.givenName = [self contactProperty:kABPersonFirstNameProperty];                   //名字
    nameObject.familyName = [self contactProperty:kABPersonLastNameProperty];                   //姓氏
    nameObject.middleName = [self contactProperty:kABPersonMiddleNameProperty];                 //名字中的信仰名称
    nameObject.namePrefix = [self contactProperty:kABPersonPrefixProperty];                     //名字前缀
    nameObject.nameSuffix = [self contactProperty:kABPersonSuffixProperty];                     //名字后缀
    nameObject.nickName = [self contactProperty:kABPersonNicknameProperty];                     //名字昵称
    nameObject.phoneticGivenName = [self contactProperty:kABPersonFirstNamePhoneticProperty];   //名字的拼音音标
    nameObject.phoneticFamilyName = [self contactProperty:kABPersonLastNamePhoneticProperty];   //姓氏的拼音音标
    nameObject.phoneticMiddleName = [self contactProperty:kABPersonMiddleNamePhoneticProperty]; //英文信仰缩写字母的拼音音标
    
    return nameObject;
}














/**
 *  获得电话号码对象数组
 */
+ (NSArray <HUContactPhoneObject *> *)contactPhoneProperty
{
    //外传数组
    NSMutableArray <HUContactPhoneObject *> * phones = [NSMutableArray arrayWithCapacity:0];
    
    //获得电话号码的多值对象
    ABMultiValueRef values = ABRecordCopyValue(self.recordRef, kABPersonPhoneProperty);
    
    for (NSInteger i = 0; i < ABMultiValueGetCount(values); i++)
    {
        HUContactPhoneObject * phoneObject = [[HUContactPhoneObject alloc]init];
        
        //开始赋值
        phoneObject.phoneTitle = (__bridge NSString *)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(values, i)); //电话描述(如住宅、工作..)
        phoneObject.phoneNumber = (__bridge NSString *)ABMultiValueCopyValueAtIndex(values, i);                                 //电话号码
        
        //添加数据
        [phones addObject:phoneObject];
    }
    
    //释放资源
    CFRelease(values);
    
    return [NSArray arrayWithArray:phones];
}




/**
 *  获得联系人的头像图片
 */
+ (UIImage *)contactHeadImagePropery
{
    //首先判断是否存在头像
    if (ABPersonHasImageData(self.recordRef) == false)//没有头像，返回nil
    {
        return nil;
    }
    
    //开始获得头像信息
    NSData * imageData = (__bridge NSData *)(ABPersonCopyImageData(self.recordRef));
    
    //获得头像原图
    //    NSData * imageData = CFBridgingRelease(ABPersonCopyImageDataWithFormat(self.recordRef, kABPersonImageFormatOriginalSize));
    
    return [UIImage imageWithData:imageData];
}













@end
